package com.entity;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for Dlyh
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.entity.Dlyh
 * @author MyEclipse Persistence Tools
 */

public class DlyhDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DlyhDAO.class);
	// property constants
	public static final String DLYHMC = "dlyhmc";
	public static final String DLYHMM = "dlyhmm";
	public static final String YHJB = "yhjb";
	public static final String ZSXM = "zsxm";
	public static final String LXFS = "lxfs";
	public static final String SFZZ = "sfzz";
	protected void initDao() {
		// do nothing
	}

	public void save(Dlyh transientInstance) {
		log.debug("saving Dlyh instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Dlyh persistentInstance) {
		log.debug("deleting Dlyh instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Dlyh findById(java.lang.Integer id) {
		log.debug("getting Dlyh instance with id: " + id);
		try {
			Dlyh instance = (Dlyh) getHibernateTemplate().get(
					"com.entity.Dlyh", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Dlyh instance) {
		log.debug("finding Dlyh instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Dlyh instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Dlyh as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByDlyhmc(Object dlyhmc) {
		return findByProperty(DLYHMC, dlyhmc);
	}

	public List findByDlyhmm(Object dlyhmm) {
		return findByProperty(DLYHMM, dlyhmm);
	}

	public List findByYhjb(Object yhjb) {
		return findByProperty(YHJB, yhjb);
	}
	public List findByLxfs(Object lxfs) {
		return findByProperty(LXFS, lxfs);
	}

	public List findByZsxm(Object zsxm) {
		return findByProperty(ZSXM, zsxm);
	}

	public List findAll() {
		log.debug("finding all Dlyh instances");
		try {
			String queryString = "from Dlyh";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Dlyh merge(Dlyh detachedInstance) {
		log.debug("merging Dlyh instance");
		try {
			Dlyh result = (Dlyh) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Dlyh instance) {
		log.debug("attaching dirty Dlyh instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Dlyh instance) {
		log.debug("attaching clean Dlyh instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DlyhDAO getFromApplicationContext(ApplicationContext ctx) {
		return (DlyhDAO) ctx.getBean("DlyhDAO");
	}
	
	/**
	 * 验证用户是否存在
	 */
	public boolean checkdlyh(String dlyhmc,String dlyhmm)
	{
		boolean b=true;
		Session session = this.getSession();
		session.beginTransaction();
		String hql = "from Dlyh where dlyhmc='"+dlyhmc+"' and dlyhmm='"+dlyhmm+"'";
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(hql);
			List list=query.list();
			if(list.size()==0)
			{
				b = false;
			}
			session.beginTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return b;
	}
	
	/**
	 * 根据用户名和密码查找用户
	 */
	public List getdlyh(String dlyhmc,String dlyhmm)
	{
		Session session = this.getSession();
		session.beginTransaction();
		String hql = "from Dlyh where dlyhmc='"+dlyhmc+"' and dlyhmm='"+dlyhmm+"'";
		List list = null;
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(hql);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}
	
	/**
	 * 根据条件查询
	 */
	public List getall(String hql)
	{
		Session session = this.getSession();
		session.beginTransaction();
		List list = null;
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(hql);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
		
	}
	
	public List getyhname(String hql)
	{
		Session session = this.getSession();
		List list = null;
		
			try {
				session.beginTransaction();
				Query query =session.createQuery(hql);
				list = query.list();
				session.beginTransaction().commit();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
	           session.close();
			}
		return list;
	}
}